﻿using BPC.Magic8Ball;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using TypeMock.ArrangeActAssert;
using System;
using Microsoft.SharePoint;

namespace BPC.Magic8Ball.MSTest
{  
    /// <summary>
    ///This is a test class for SharePointTest and is intended
    ///to contain all SharePointTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SharePoint
    {
        Magic8Ball.SharePoint _sharePoint;
        private TestContext testContextInstance;

        public SharePoint()
        {
            _sharePoint = new BPC.Magic8Ball.SharePoint();
            FakeList();
        }
        
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]
        public void ShouldReturnListOfAnswersFromSharePointList()
        {
            //Arrange

            //Act
            List<String> answers = _sharePoint.GetAnswersFromList("ListName");

            //Assert
            Assert.IsNotNull(answers);
        }

        [TestMethod]
        public void ShouldReturnSpecifcAnswerFromSharePointList()
        {
            //Arrange

            //Act
            List<String> answers = _sharePoint.GetAnswersFromList("ListName");

            //Assert
            Assert.AreEqual("AnswerOne", answers[1]);
        }


        //Fake the SharePoint List values and Context
        private void FakeList()
        {
            //Isolate.Fake.StaticMethods<SPContext>(Members.ReturnRecursiveFakes);

            SPList fakeList = Isolate.Fake.Instance<SPList>();
            Isolate.WhenCalled(() => SPContext.Current.Web.Lists["ListName"]).WillReturn(fakeList);

            //Now fake the list return values
            SPListItem fakeAnswerOne = Isolate.Fake.Instance<SPListItem>();
            SPListItem fakeAnswerTwo = Isolate.Fake.Instance<SPListItem>();


            Isolate.WhenCalled(() => fakeAnswerOne.Title).WillReturn("AnswerZero");
            Isolate.WhenCalled(() => fakeAnswerTwo.Title).WillReturn("AnswerOne");

            Isolate.WhenCalled(() => fakeList.Items).WillReturnCollectionValuesOf(new List<SPItem>
                                  {
                                      fakeAnswerOne,
                                      fakeAnswerTwo
                                  });
        }


    }
}
